package com.tbs.parkingsaleapp.UI;

import abstergo.tbs.betterasyc.AsycBase.AsycWorker;
import abstergo.tbs.betterasyc.Handler.HandlerMananger;
import abstergo.tbs.betterasyc.Handler.IHandlerAction;
import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.tbs.parkingsaleapp.Http.JsonData.BasicOrder;
import com.tbs.parkingsaleapp.Http.JsonData.DiscountInfo;
import com.tbs.parkingsaleapp.Http.JsonData.OrderInfo;
import com.tbs.parkingsaleapp.Http.Listenner.DiscountGetter;
import com.tbs.parkingsaleapp.Http.Listenner.UseDiscountHttper;
import com.tbs.parkingsaleapp.R;
import com.tbs.parkingsaleapp.SystemManager.Handler.HandlerConst;
import com.tbs.parkingsaleapp.UI.Adapters.DiscountAdapter;
import com.tbs.parkingsaleapp.UI.Listenners.DiscountCheckedListenner;

import java.util.ArrayList;

public class DiscountApplyActivity extends AppCompatActivity {

    public static final String k_ORDER="ORDER_KEY";

    static ArrayList<DiscountInfo> checked=new ArrayList<>();

    public static void CheckItem(int pos)
    {
        checked.add((DiscountInfo) adapter.getItem(pos));
    }
    public static void UncheckItem(int pos)
    {
        DiscountInfo info= (DiscountInfo) adapter.getItem(pos);
        for (DiscountInfo o:checked
             ) {
            if(o.getDiscount().getId()==info.getDiscount().getId())
            {
                checked.remove(o);
                break;
            }
        }
    }
    public static Context called;

    public static void ActivityStart(Context context, OrderInfo order)
    {
        checked.clear();
        called=context;
        Intent intent=new Intent();
        intent.setClass(context,DiscountApplyActivity.class);
        intent.putExtra(k_ORDER,order);
        context.startActivity(intent);
    }

    TextView price;
    public  void setNEWPrice(double p)
    {
        price.setText(String.format("价格：%.2f",p));
    }
     void InitOrderPart(OrderInfo info)
    {
        View root=findViewById(R.id.discount_id);
        TextView textView=root.findViewById(R.id.order_id);
        textView.setText("订单号："+info.getBasic().getId());
        textView=root.findViewById(R.id.order_time);
        textView.setText("下单时间："+info.getBasic().getTime());
        textView=root.findViewById(R.id.order_location);
        textView.setText("小区地址："+info.getLocationInfo().getAddress());
        textView=root.findViewById(R.id.order_locationname);
        textView.setText("小区名称"+info.getLocationInfo().getName());
        price=root.findViewById(R.id.order_price);
        price.setText(String.format("价格：%.2f",info.getBasic().getPrice()));
        textView=root.findViewById(R.id.order_status);
        String status="状态：";
        switch (info.getBasic().getStatus())
        {
            case 0:
                status+="订单审核";
                break;
            case 1:
                status+="订单交易成功";
                break;
            case 2:
                status+="摇号成功";
                break;
            case -1:
                status+="订单关闭";
                break;
        }
        textView.setText(status);
        textView=root.findViewById(R.id.order_parknum);
        textView.setText("车位号:"+info.getBasic().getNumber());
        textView=root.findViewById(R.id.order_comname);
        textView.setText("所属公司:"+info.getCompanyInfo().getName());
    }
    OrderInfo PickData()
    {
       return  (OrderInfo) getIntent().getSerializableExtra(k_ORDER);
    }

    static DiscountApplyActivity instance;

    public static DiscountApplyActivity getInstance() {
        return instance;
    }
    public Button btn;
    static int time=0;

    public  void setTime(int time) {
        DiscountApplyActivity.time = time;
        if(time>1)
            time=0;
        switch (time)
        {
            case 0:
                btn.setText("拟使用");
                break;
            case 1:
                btn.setText("确认使用");
                break;
        }
    }

    static ListView ls;
    static DiscountAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.discount_layout);
        OrderInfo info= PickData();
        InitOrderPart(info);
        ls=findViewById(R.id.discount_ls);
        ls.setBackground(getDrawable(R.drawable.blank));
        ls.setDivider(getDrawable(R.drawable.blank));
        ls.setDividerHeight(55);
        ls.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        ls.setOnItemClickListener(new DiscountCheckedListenner(this));

        HandlerMananger.Subscribe(HandlerConst.UseableDiscountGET, new IHandlerAction() {
            @Override
            public void Handle(Message rawmsg) {
                adapter=new DiscountAdapter((DiscountInfo[]) rawmsg.obj,ls);
                ls.setAdapter(adapter);
            }
        },true);
        DiscountGetter getter=new DiscountGetter();
        AsycWorker<Integer,DiscountInfo[]> asycWorker =new AsycWorker<Integer,DiscountInfo[]>().setContext(this).setBackgroundAction(getter).setResultAction(getter);
        asycWorker.Init(1,true).Run(info.getBasic().getId());
        btn=findViewById(R.id.discount_update);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                UseDiscountHttper httper=new UseDiscountHttper();
                AsycWorker<Integer, BasicOrder> http=new AsycWorker<Integer, BasicOrder>().setResultAction(httper).setBackgroundAction(httper).setContext(DiscountApplyActivity.this);

                httper.setDiscount(called,checked);
                http.Init(1,true).Run(info.getBasic().getId(),time);
                setTime(time+1);
                v.setEnabled(false);

            }
        });
        instance=this;
    }

}